import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
// 引入 mock 插件
import { viteMockServe } from "vite-plugin-mock";
// 引入 svg 插件
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";

// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
  return {
    plugins: [
      vue(),
      // svg icon 插件
      createSvgIconsPlugin({
        // 指定存放 svg icon 的目录为 src/assets/icons
        iconDirs: [path.resolve(process.cwd(), "src/assets/icons")],

        symbolId: "icon-[dir]-[name]",
      }),
      viteMockServe({
        // 指定 mock 接口的文件夹
        mockPath: "mock",
        // 开发环境开启
        localEnabled: command === "serve",
        // 生产环境关闭
        prodEnabled: false,
      }),
    ],
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
      },
    },
    // scss 全局变量配置
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: `@import "@/styles/variable.scss";`,
        },
      },
    },
    // devServer
    server: {
      port: 3000,
      open: true,
      proxy: {
        "/api": {
          target: "http://localhost:9000",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
    },
  };
});
